//
//  TableViewController.m
//  tableView
//
//  Created by dally with god on 16/11/14.
//  Copyright © 2016年 1512. All rights reserved.
//

#import "TableViewController.h"

#import "NumberGroup.h"

#import "MYCell.h"

@interface TableViewController ()<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic,strong)NSMutableArray *dataList;

@end


@implementation TableViewController
-(NSMutableArray*)dataList {
    if(_dataList ==nil) {
        _dataList = [NSMutableArray array ];
        
        for (int i=0 ;i<4 ;i++) {
            NumberGroup *numberGroup = [NumberGroup numberGroup ];
                                        [_dataList addObject:numberGroup];
        }
    }
    return _dataList;
}


-(void)viewDidLoad{
    [super viewDidLoad];
    
    self.tableView.sectionIndexColor = [UIColor redColor];
    self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    
    
    //设置cell 的高度
    //动态方法
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight= UITableViewAutomaticDimension;
    //普通
    //self.tableView.rowHeight = 80;
}

#pragma mark -数据源方法实现

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //取出对应的section
    NumberGroup *tempGroup = self.dataList[section];
    
    return tempGroup.groupNumbers.count;
}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //初始化
    MYCell *cell = [MYCell mycellWithTableView:tableView];
    
    //从模型中取出数据
    NumberGroup *tempGroup = self.dataList[indexPath.section];
    NSString *numberString = tempGroup.groupNumbers[indexPath.row];
    
    //传递数据给view
    cell.cellData = numberString;
    
    //返回
    return cell;
    
    
//   //定义一个静态标示符
//    static NSString *CellID=@"%d";
//    
//    //优先从缓存池中查找id伟cell 的单元格
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
//    //如果缓存池中没有则创建
//    if (cell  ==nil ) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
//    }
//    //UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewRowActionStyleDefault reuseIdentifier:nil ];
//    
//    
//    //获取数据源
//    NumberGroup  *tempGroup = self.dataList[indexPath.section];
//    NSString *numberString = tempGroup.groupNumbers[indexPath.row];
//    
//    //设置cell的内容
//    
//    
//    cell.textLabel.text = numberString;
//    cell.detailTextLabel.text  =numberString;
//    cell.imageView.image=[UIImage imageNamed:@"image"];
//    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//    
//    NSLog(@"cell:%@",cell);
//    
//    return cell;
//    
}


#pragma mark - 单元格的点击方法
//- (nullable NSIndexPath *) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"%s",__func__);
//}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *infoString = [NSString stringWithFormat:@"第%d段,第%d行",indexPath.section,indexPath.row];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:infoString delegate:nil  cancelButtonTitle:nil  otherButtonTitles:@"ok", nil];
    [alertView show];
}


#pragma  mark -单元格的编辑
-(nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"刷新" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [tableView reloadData];
    }];
    
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self.dataList removeObjectAtIndex:indexPath.row];
        
        //刷新表格
        [tableView reloadData];
    }];
    
    NSArray *actionArray = @[action1,action2];
    return actionArray;
    
}


//打印日志
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s",__func__);
}
-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s",__func__);
}


#pragma mark -title 
-(nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NumberGroup *numberGroup = self.dataList[section];
    return numberGroup.gourpHeader;
}

-(nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    NumberGroup *numberGroup =self.dataList[section];
    return numberGroup.gourpFooter;
}

#pragma  mark -index 
-(nullable NSArray<NSString *> *)
sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *indexArray = [NSMutableArray array];
    for (NumberGroup *numberGroup in self.dataList){
        [indexArray addObject:numberGroup.gourpIndex];
    }
    return indexArray;
}

#pragma  mark -定制section的header和footer
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
    imageView.image = [UIImage imageNamed:@"6-1202131024400"];
                       
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 100, 20)];
    lable.text = @"猜你喜欢";
    lable.font = [UIFont systemFontOfSize:14.0];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(250, 5, 60, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"u=3584221397,3748333969&fm=21&gp=0" ]  forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickOpenButton)  forControlEvents:UIControlEventTouchUpInside];
    
    UIView *headerView = [[UIView alloc]init];
    [headerView addSubview:imageView];
    [headerView addSubview:lable];
    [headerView addSubview:button];
    
    headerView.backgroundColor = [UIColor whiteColor];
    return  headerView;
}

-(void) clickOpenButton {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"点击了open" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK" , nil];
    [alertView show];
}


#pragma  mark -cell高度设置
/*-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  120;
}
*/
#pragma  mark -动态调整cell高度 （self-sizing）
/*使用xib搭建cell并设置约束
编写代码

self.UITableView.estimatedRowHeight = 67;
self.tableView.rowHeight = UITableViewMutomaticDimenaion;
*/
@end
